package com.eling.elcms.travel.service.impl;

import com.eling.elcms.basedoc.model.BusinessMessage;
import com.eling.elcms.basedoc.model.TargetType;
import com.eling.elcms.basedoc.model.TaskType;
import com.eling.elcms.basedoc.service.IBusinessMessageManager;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.travel.dao.INonMemGroupReserveDao;
import com.eling.elcms.travel.model.CheckInImplement;
import com.eling.elcms.travel.model.CheckInImplement.CheckStatus;
import com.eling.elcms.travel.model.CheckInImplement.DataSource;
import com.eling.elcms.travel.model.NonMemGroupReserve;
import com.eling.elcms.travel.model.NonMemberGroup;
import com.eling.elcms.travel.model.NonMemberGroup.SignUpType;
import com.eling.elcms.travel.model.NonMemberGroup.Status;
import com.eling.elcms.travel.model.SignUpRoom;
import com.eling.elcms.travel.model.view.NonMemberGroupView;
import com.eling.elcms.travel.service.ICheckInImplementManager;
import com.eling.elcms.travel.service.INonMemGroupReserverManager;
import com.eling.elcms.travel.service.INonMemberGroupManager;
import com.eling.elcms.travel.service.ISignUpRoomManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Service
public class NonMemGroupReserveManagerImpl extends GenericManagerImpl<NonMemGroupReserve, Long>
		implements INonMemGroupReserverManager {

	INonMemGroupReserveDao nonMemGroupReserveDao;

	@Autowired
	private INonMemberGroupManager nonMemGroupManager;

	@Autowired
	private ISignUpRoomManager signUpRoomManager;

	@Autowired
	private ICheckInImplementManager cIImplementM;
	
	@Autowired
	private IBusinessMessageManager businessMessageManager;

	@Autowired
	public void setNonMemberRegisterDao(INonMemGroupReserveDao dao) {
		this.dao = dao;
		this.nonMemGroupReserveDao = dao;
	}

	@Override
	public NonMemGroupReserve reserve(NonMemberGroupView cond) {
		NonMemberGroup group = nonMemGroupManager.get(cond.getNonMemberGroup().getPkNonMemberGroup());
		group.setStatus(cond.getNonMemberGroup().getStatus());
		group = nonMemGroupManager.save(group);
		// 根据主表PK得到对应预约
		NonMemGroupReserve reserve = new NonMemGroupReserve();
		PropertyUtils.setProperty(reserve, "nonMemberGroup.pkNonMemberGroup",
				cond.getNonMemberGroup().getPkNonMemberGroup());
		List<NonMemGroupReserve> reserves = query(reserve);
		// 得到前台传来的散客预约房型集合
		List<SignUpRoom> list = cond.getSignUpRooms();
		// 保存散客预约房型集合
		list = signUpRoomManager.save(list);
		if (!reserves.isEmpty()) {
			reserve = reserves.get(0);
		} else {
			reserve = new NonMemGroupReserve();
		}
		reserve.setSignUpRooms(new HashSet<SignUpRoom>(list));
		reserve.setNonMemberGroup(group);

		// 推入住准备落实单
		reserve = save(reserve);
		if (reserve.getNonMemberGroup().getStatus().equals(Status.Reserve)) {
			sendCheckInOrder(reserve);
		}

		return reserve;
	}

	/**
	 * 推送入住准备落实单
	 * 
	 * @author zhoulin
	 * @time 2016年10月12日 10:33:26
	 */
	@Override
	public void sendCheckInOrder(NonMemGroupReserve reserve) {
		CheckInImplement cii = new CheckInImplement();
		cii.setNonMemGroupReserve(reserve);
		List<CheckInImplement> ciiList = cIImplementM.query(cii);
		// 该预约没有落实单，且体验团状态为有效，或者散客团状态为已收费和已预约时推送落实单
		Status status = reserve.getNonMemberGroup().getStatus();
		if (ciiList.isEmpty() && ((status.equals(Status.Effective)
				&& reserve.getNonMemberGroup().getSignUpType().equals(SignUpType.Experience))
				|| (reserve.getNonMemberGroup().getSignUpType().equals(SignUpType.NonMember)
						&& (status.equals(Status.Effective) || status.equals(Status.Reserve) || status.equals(Status.Confirm))))) {
			cii.setServicePoint(reserve.getNonMemberGroup().getServicePoint());
			cii.setStatus(CheckStatus.Initial);
			cii.setRoomStatus(CheckStatus.Pending);
			cii.setDataSource(DataSource.NonMember);
			cii.setConfirmStatus(CheckInImplement.ConfirmStatus.NoConfirm);
			cii.setVersion(0);
			cii = cIImplementM.save(cii);
			BusinessMessage bm = new BusinessMessage(TargetType.Role, TaskType.CheckInImplement, cii.getPkCIImplement());
			businessMessageManager.saveBusinessMessage(bm);
		}
	}

}
